package com.monicaca.controller;


import com.monicaca.bean.CKD;
import com.monicaca.bean.WLK;
import com.monicaca.bean.INV;
import com.monicaca.bean.INV_LOG;
import com.monicaca.bean.RKD;
import com.monicaca.service.CKDService;
import com.monicaca.service.INVService;
import com.monicaca.service.INV_LOGService;
import com.monicaca.service.WLKService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/CKD")
public class CKDController {
    @Autowired
    CKDService ckdService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    INVService invService;
    @Autowired
    INV_LOGService inv_logService;
    @Autowired
    WLKService wlkService;
    
    
    
    @GetMapping("/")
    public String indexView(
            Model model){
        model.addAttribute("CKD",ckdService.findAll());
        return "CKD";
    }
    
    @GetMapping("/up/{cid}")
    public String up(Model model, @PathVariable int cid){
		CKD ckd=ckdService.findByCKDId(cid);
		int id=ckd.getId();
		
		if(ckd.getZT().equals("完成")==true){
			model.addAttribute("CKD",ckdService.findAll());
    		model.addAttribute("msg","该出库单已提交过，不要重复提交！"); 
    		return "CKD";			
		}
		
		
		INV reluser=invService.findByInvId(id);
		if(reluser==null){
    		model.addAttribute("msg","库存不足，提交失败"); 
    		model.addAttribute("CKD",ckdService.findAll());
    		return "CKD";
    	}
		int a=reluser.getNum();
    	int b=ckd.getSL();
    	if(a<b){
    		model.addAttribute("msg","库存不足，提交失败");  
    		model.addAttribute("CKD",ckdService.findAll());
    		return "CKD";
    	}
    		
    	ckd.setZT("完成");
		ckdService.updateCKD(ckd);
    	
		reluser.setNum(a-b);
		invService.updateINV(reluser);
		 
		INV_LOG inv_log=new INV_LOG();
	        
        inv_log.setId(ckd.getId());  
        inv_log.setNum(ckd.getSL());
        inv_log.setWL(ckd.getWL());
        inv_log.setDW(ckd.getDW());
        inv_log.setAction("减少");
        
        inv_logService.createINV_LOG(inv_log);
        model.addAttribute("msg","提交成功");       	      		  
        model.addAttribute("CKD",ckdService.findAll());
	
		
    	return "CKD";   
    }
    
    @PostMapping("/update/{id}")
    public String updateView(
            Model model, @PathVariable int id,CKD ckd){
        CKD realuser=ckdService.findByCKDId(id); 
        realuser.setSL(ckd.getSL());        
        ckdService.updateCKD(realuser);
        model.addAttribute("msg","修改成功");
        model.addAttribute("CKD",ckdService.findAll());
        return "CKD";
    }
    
    @PostMapping("/save")
    public String updateView2(
            Model model, CKD ckd){
    	
    	
        	int id=ckd.getId();
        	WLK wl=wlkService.findByRKDId(id);
        	
        	
        	INV reluser=invService.findByInvId(id);
        
        		 ckdService.createCKD(ckd);
        		 ckd.setDW(wl.getDW());
        		 ckd.setWL(wl.getWL());
        		 Date day=new Date();    
        		 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //自动获取系统时间
        		 ckd.setTime(df.format(day));
        		 ckd.setZT("初始");
        		 ckdService.updateCKD(ckd);
        		 
        		 
        		 
        		 
        		 
		 
		       
		       
                 model.addAttribute("msg","新增成功");       	      		  
        model.addAttribute("CKD",ckdService.findAll());
        return "CKD";
    }
    
    

    
    @GetMapping("/add")
    public String addView(
            Model model){
        return "CKDadd";
    }
    @GetMapping("/delete/{cid}")
    public String deteleView(
            Model model, @PathVariable int cid){


    	CKD ckd=ckdService.findByCKDId(cid);
    	String ZT=ckd.getZT();
    	if(ZT.equals("完成")==true){
   		 model.addAttribute("msg","出库单已完成，不能删除！");
   	        model.addAttribute("CKD",ckdService.findAll());
   	        return "CKD";
    	}
        ckdService.deleteCKD(cid);
        model.addAttribute("msg","删除成功");
        model.addAttribute("CKD",ckdService.findAll());
        return "CKD";
    }
    @GetMapping("/change/{cid}")
    public String changeView(
            Model model, @PathVariable int cid){
    	
    	
    	CKD ckd=ckdService.findByCKDId(cid);
    	String ZT=ckd.getZT();
    	if(ZT.equals("完成")==true){
   		 model.addAttribute("msg","出库单已完成，不能修改！");
   	        model.addAttribute("CKD",ckdService.findAll());
   	        return "CKD";
   	} 	
        model.addAttribute("cid",cid);
        
        return "CKDchange";
    }

}
